#!/bin/bash
#
# kopano-server Kopano Core Storage Server
#
# chkconfig: 345 85 25
# processname: /usr/sbin/kopano-server
# config: /etc/kopano/server.cfg
# pidfile: /var/run/kopano/server.pid

### BEGIN INIT INFO
# Provides: kopano-server
# Required-Start: $local_fs $network $remote_fs $syslog
# Required-Stop: $local_fs $network $remote_fs $syslog
# Should-Start: mysqld
# Should-Stop: mysqld
# Short-Description: Kopano Core Storage Server
### END INIT INFO

SERVERCONFIG=/etc/kopano/server.cfg
SERVERPROGRAM=/usr/sbin/kopano-server

# Sanity checks.
[ -x $SERVERPROGRAM ] || exit 0

SERVERCONFIG_OPT=""
[ ! -z $SERVERCONFIG -a -f $SERVERCONFIG ] && SERVERCONFIG_OPT="-c $SERVERCONFIG"

[ -f /etc/sysconfig/kopano ] && . /etc/sysconfig/kopano
export KOPANO_USERSCRIPT_LOCALE

# Source function library.
. /etc/rc.d/init.d/functions
if [ -z "$KOPANO_LOCALE" ]; then
	KOPANO_LOCALE="C"
fi

RETVAL=0
server=`basename $SERVERPROGRAM`
lockfile=/var/lock/subsys/$server
pidfile=/var/run/kopano/server.pid

start() {
	# Start in background, always succeeds
	echo -n $"Starting $server: "
	install -dm0775 -o kopano -g kopano /var/run/kopano
	export LC_ALL=$KOPANO_LOCALE
	export LANG=$KOPANO_LOCALE
	daemon $SERVERPROGRAM $SERVERCONFIG_OPT
	RETVAL=$?
	unset LC_ALL LANG
	echo
	[ $RETVAL -eq 0 ] && touch $lockfile

	return $RETVAL
}

stop() {
	if [ -f /tmp/zarafa-upgrade-lock -o -f /tmp/kopano-upgrade-lock ]; then
		echo
		echo "Server database upgrade is taking place."
		echo "Do not stop this process bacause it may render your database unusable."
		echo
		exit 1
	fi
	# Cannot use killproc, because it has no timeout feature;
	# the server may take up to 60 seconds to shutdown.
	SPID=`cat /var/run/kopano/server.pid 2>/dev/null`
	if [ -z "$SPID" -o ! -n "$SPID" ]; then
		echo -n "Program ID of $server not found, trying to stop anyway: "
		killall $SERVERPROGRAM >/dev/null 2>&1
		RETVAL=$?
		echo
		if [ $RETVAL -eq 0 ]; then
			failure $"Stopping $server: "
		else
			success $"Stopping $server: "
		fi
		RETVAL=0
	else
		echo -n $"Stopping $server: "
		TIMEOUT=65
		/bin/kill $SPID >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			while [ $TIMEOUT -gt 0 ]; do
				/bin/kill -0 $SPID >/dev/null 2>&1 || break
				sleep 1
				TIMEOUT=$[${TIMEOUT}-1]
			done
			if [ $TIMEOUT -eq 0 ]; then
				failure $"Timeout on stopping $server"
				RETVAL=1
			else
				success $"Stopping $server: "
				RETVAL=0
			fi
		else
			failure $"Stopping $server: "
		fi
		echo
	fi
	[ $RETVAL -eq 0 ] && rm -f $lockfile $pidfile

	return $RETVAL
}

restart() {
	stop
	start
}

reload() {
	echo -n $"Restarting $server: "
	killproc $SERVERPROGRAM -SIGHUP
	RETVAL=$?
	echo

	return $RETVAL
}

# See how we were called.
case "$1" in
    start)
		start
		;;
    stop)
		stop
		;;
    status)
		status $server
		RETVAL=$?
		;;
    restart|force-reload)
		restart
		;;
    condrestart|try-restart)
		if [ -f ${pidfile} ]; then
			stop
			start
		fi
		;;
    reload)
		reload
		;;
    *)
		echo $"Usage: $server {start|stop|status|reload|restart|condrestart|force-reload|try-restart}"
		RETVAL=1
		;;
esac

exit $RETVAL
